package si.yun.utils;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * @ClassName: guava缓存工具类
 * @Description: TODO
 * @Author: 李振海
 * @Date 2021/7/28 12:48
 */
@Component
public class GuavaUtils {
    private Cache<String,Object> tenCacheBuild= CacheBuilder.newBuilder()
            //初始化容量
            .initialCapacity(5)
            //容量最大值
            .maximumSize(100)
            //设置并发数
            .concurrencyLevel(5)
            //设置缓存的过期时间
            .expireAfterWrite(600, TimeUnit.SECONDS)
            .recordStats()
            .build();

    public Cache<String, Object> getTenCacheBuild() {
        return tenCacheBuild;
    }

    public void setTenCacheBuild(Cache<String, Object> tenCacheBuild) {
        this.tenCacheBuild = tenCacheBuild;
    }

    //删除缓存内容
    public void invalidateAll() {
        this.tenCacheBuild.invalidateAll();
    }

    //删除单个缓存内容
    public void invalidate(String  name) {
        this.tenCacheBuild.invalidate(name);
    }

}